/**
 * @description 菜单管理控制器
 * @author 韩
 */

const { randomUUID } = require("crypto")
const dayjs = require("dayjs")
const brand_model = require("../../../model/product/brand/BRAND.MODEL")
const { getFilter } = require("../../../utils/setFilter")
// 创建一个内容
const brand_controller_save = async ({
    id = undefined,
    brandName = undefined,
    brandIcon = undefined,
    classId = undefined,
    sort = undefined,
    status = undefined
}) => {
    const params = {
        id: randomUUID(), brandName, brandIcon: JSON.stringify(brandIcon), classId: JSON.stringify(classId), sort, status
    }
    console.log("要保存的参数：", params)
    let addRes = null;
    if (!id) {
        // 添加创建
        addRes = await brand_model.create({
            ...params
        })
    } else {
        // 更新修改
        addRes = await brand_model.findOneAndUpdate({ id: id }, {
            $set: {
                ...params
            }
        }, { new: true })
    }

    if (addRes) {
        const {
            id, brandName, brandIcon, classId, sort, status
        } = addRes

        return {
            code: 0,
            msg: "成功",
            data: {
                id, brandName, brandIcon: JSON.parse(brandIcon), classId: JSON.parse(classId), sort, status
            }
        }
    }
    return {
        code: "2000",
        msg: "失败",
    }
}

// 查询列表 
const brand_controller_find = async ({ current, pageSize, brandName = null, status = undefined, classId = undefined }) => {
    try {
        let filter = {
            brandName: brandName && { $regex: eval("/" + brandName + "/i") },//名称模糊查询
            classId: classId && { $regex: eval("/" + classId + "/i") },//名称模糊查询
            status: status && { $in: status },
            // classId: classId && { $in: classId }
        }
        filter = getFilter(filter)
        const list = await brand_model.find(filter).sort({ sort: 1 }).skip((current - 1) * pageSize).limit(pageSize).lean();
        const totalCount = await brand_model.find(filter).countDocuments();
        return {
            code: 0,
            msg: "查询成功",
            data: {
                list,
                totalCount
            }
        }
    } catch (error) {
        return {
            code: 100,
            msg: "失败",
        }
    }
}


/**@description 根据id查详情 */
const brand_controller_get = async ({ id }) => {
    const res = await brand_model.findOne({
        id
    })
    if (res) {
        const { id, brandName, brandIcon, classId, sort, status } = res;
        return {
            code: 0,
            msg: "查询成功",
            data: {
                id, brandName, brandIcon: JSON.parse(brandIcon), classId: JSON.parse(classId), sort, status
            }
        }
    } else {
        return {
            code: '01',
            msg: "未找到对应菜单数据"
        }
    }
}

// 删除
const brand_controller_delete = async ({ ids = [] }) => {
    const deleteResult = await brand_model.deleteMany({
        id: { $in: ids }
    });

    if (deleteResult.deletedCount) {
        return {
            code: 0,
            msg: `删除了${deleteResult.deletedCount}条数据`
        }
    } else {
        return {
            code: 12,
            msg: `删除失败`
        }
    }

}
module.exports = {
    brand_controller_save,
    brand_controller_find,
    brand_controller_get,
    brand_controller_delete,
}